<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Language" content="zh-cn">
    
    
    <title>
        
            LeetCode笔记-动态规划 | Nefelibata
             | Nefelibata
        
    </title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    
        <meta name="theme-color" content="#77AAFF">
    
    
    <meta name="keywords" content="Leetcode">
    
    

    

    <!-- Baidu Push -->
<script>
	(function(){
		var bp = document.createElement('script');
		var curProtocol = window.location.protocol.split(':')[0];
		if (curProtocol === 'https') {
			bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
		}
		else {
			bp.src = 'http://push.zhanzhang.baidu.com/push.js';
		}
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(bp, s);
	})();

	var _hmt = _hmt || [];
</script>



    
    <meta name="description" content="动规">
<meta property="og:type" content="article">
<meta property="og:title" content="LeetCode笔记-动态规划">
<meta property="og:url" content="https://nefelibata.icu/2022/0826e7783f.html">
<meta property="og:site_name" content="Nefelibata">
<meta property="og:description" content="动规">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://code-thinking-1253855093.file.myqcloud.com/pics/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%981.jpeg">
<meta property="article:published_time" content="2022-08-06T09:41:48.000Z">
<meta property="article:modified_time" content="2024-01-04T02:41:48.345Z">
<meta property="article:author" content="Nefelibata">
<meta property="article:tag" content="Leetcode">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://code-thinking-1253855093.file.myqcloud.com/pics/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%981.jpeg">
    
        <link rel="alternate" type="application/atom+xml" title="Nefelibata" href="/blog/atom.xml">
    
    <link rel="shortcut icon" href="/blog/img/favicon.ico">
    <link id="style" rel="stylesheet" href="/blog/css/style.css?v=3.0">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    
<meta name="generator" content="Hexo 6.3.0">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body>
    <div id="loading" class="active"></div>
    <aside id="menu"  class >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" >
      <div class="brand">
        <a class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg" alt="avatar">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname" id="name">Nefelibata</h5>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
              <li class="waves-block waves-effect">
                  <a href="/blog/" target="_self" >
                    <i class="icon icon-lg icon-home"></i>
                    <span>主 页</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/archives" target="_self" >
                    <i class="icon icon-lg icon-archives"></i>
                    <span>归 档</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/categories" target="_self" >
                    <i class="icon icon-lg icon-th-list"></i>
                    <span>分 类</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/tags" target="_self" >
                    <i class="icon icon-lg icon-tags"></i>
                    <span>标 签</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/../gallery" target="_blank" >
                    <i class="icon icon-lg icon-image"></i>
                    <span>相册</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/../about" target="_blank" >
                    <i class="icon icon-lg icon-smile-o"></i>
                    <span>关 于</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/../" target="_self" >
                    <i class="icon icon-lg icon-tree"></i>
                    <span>Nefelibata</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
      <div class="nav2">
          
              <a class="nav2item" data-title="Email" href="mailto:kunpengl0111@gamil.com" target="_parent"title="Email" >
                <i class="icon icon-lg icon-envelope-o envelope-o"></i>
              </a>
          
              <a class="nav2item" data-title="Github" href="https://github.com/kpl0111" target="_blank"title="Github" >
                <i class="icon icon-lg icon-github github"></i>
              </a>
          
              <a class="nav2item" data-title="Twitter" href="https://twitter.com/Nefelib31847846" target="_blank"title="Twitter" >
                <i class="icon icon-lg icon-twitter twitter"></i>
              </a>
          
        </div>
      </ul>
    </div>
  </div>
</aside>


    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">LeetCode笔记-动态规划</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        <a href="../../atom.xml" target="_blank" class="header-icon waves-effect waves-circle waves-light" id="Rss">
            <i class="icon icon-lg icon-rss"></i>
        </a>
    </div>
</header>
<header class="content-header post-header">
    <div class="container fade-scale">
        <div id="myheader">
            <h1 class="title">
                
            </h1>
            <h5 class="subtitle">
                
                
            </h5>
        </div>
    </div>
</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            
                <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#%E5%8A%A8%E8%A7%84%E7%9A%84%E4%BA%94%E9%83%A8%E6%9B%B2"><span class="post-toc-text">动规的五部曲</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2022-8-9"><span class="post-toc-text">2022.8.9</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%982022-08-10"><span class="post-toc-text">背包问题2022.08.10</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#01%E8%83%8C%E5%8C%85%E5%92%8C%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%852022-08-13"><span class="post-toc-text">01背包和完全背包2022.08.13</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#01%E8%83%8C%E5%8C%85-%E4%BA%8C%E7%BB%B4-%E5%85%88%E7%89%A9%E5%93%81%E5%90%8E%E8%83%8C%E5%8C%85"><span class="post-toc-text">01背包-二维-先物品后背包</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#01%E8%83%8C%E5%8C%85-%E4%BA%8C%E7%BB%B4-%E5%85%88%E8%83%8C%E5%8C%85%E5%90%8E%E7%89%A9%E5%93%81"><span class="post-toc-text">01背包-二维-先背包后物品</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#01%E8%83%8C%E5%8C%85-%E4%B8%80%E7%BB%B4"><span class="post-toc-text">01背包-一维</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85"><span class="post-toc-text">完全背包</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#%E5%85%B3%E4%BA%8E%E6%8E%92%E5%88%97%E5%92%8C%E7%BB%84%E5%90%88"><span class="post-toc-text">关于排列和组合</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#%E5%A4%9A%E9%87%8D%E8%83%8C%E5%8C%85"><span class="post-toc-text">多重背包</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#%E8%83%8C%E5%8C%85%E6%80%BB%E7%BB%93"><span class="post-toc-text">背包总结</span></a></li></ol></li></ol>
            
        </nav>
    </aside>
   
<article id="post-LeetCode笔记-动态规划"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">LeetCode笔记-动态规划</h1>
        <div class="post-meta">
            <i class="icon icon-lg icon-calendar-o"></i>
            发表于
            <time class="post-time" title="2022-08-06 17:41:48" datetime="2022-08-06T09:41:48.000Z"  itemprop="datePublished">2022-08-06</time>

            <br id="mybreak"/>
            
	<i class="icon icon-lg icon-folder-o"></i>
	分类：<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E7%AC%94%E8%AE%B0/">笔记</a></li></ul>


            <i>·</i>
        </div>
        <div class="post-count-custom">
            <i class="icon icon-lg icon-comment-o"></i>
            阅读本文可能花费您&nbsp;<span class="post-count">10.3</span>&nbsp;分钟
        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>动规</p>
<span id="more"></span>

<blockquote>
<p>本篇为个人笔记，内容或有错误。<br>图片部分源于<a target="_blank" rel="noopener" href="https://programmercarl.com/">代码随想录</a>，侵删。</p>
</blockquote>
<blockquote>
<p>动态规划，英文：Dynamic Programming，简称DP，如果某一问题有很多重叠子问题，使用动态规划是最有效的。<br>所以动态规划中每一个状态一定是由上一个状态推导出来的，这一点就区分于贪心，贪心没有状态推导，而是从局部直接选最优的</p>
</blockquote>
<h2 id="动规的五部曲"><a href="#动规的五部曲" class="headerlink" title="动规的五部曲"></a>动规的五部曲</h2><ol>
<li>确定dp数组（dp table）以及下标的含义</li>
<li>确定递推公式</li>
<li>dp数组如何初始化</li>
<li>确定遍历顺序</li>
<li>举例推导dp数组</li>
</ol>
<p><strong>如果代码写出来了，一直AC不了，灵魂三问：</strong></p>
<ol>
<li>这道题目我举例推导状态转移公式了么？</li>
<li>我打印dp数组的日志了么？</li>
<li>打印出来了dp数组和我想的一样么？</li>
</ol>
<p>首先要明白dp数组的含义是什么，下表代表什么，确定了这个之后按照套路走就行，手动推导dp数组本质上也是一种检验的过程，当打印出结果符合预期手动推导结果时，基本就没太大问题</p>
<h2 id="2022-8-9"><a href="#2022-8-9" class="headerlink" title="2022.8.9"></a>2022.8.9</h2><p>动态规划的习题我认为最重要的就是要确定dp数组的定义，下标代表什么，该如何初始化，举个例子：</p>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/integer-break/">LeetCode304：整数拆分</a></p>
<blockquote>
<p>给定一个正整数&nbsp;n，将其拆分为至少两个正整数的和，并使这些整数的乘积最大化。 返回你可以获得的最大乘积。</p>
<p>示例 1:</p>
<ul>
<li>输入: 2</li>
<li>输出: 1</li>
<li>解释: 2 = 1 + 1, 1 × 1 = 1。</li>
</ul>
<p>示例&nbsp;2:</p>
<ul>
<li><p>输入: 10</p>
</li>
<li><p>输出: 36</p>
</li>
<li><p>解释: 10 = 3 + 3 + 4, 3 ×&nbsp;3 ×&nbsp;4 = 36。</p>
</li>
<li><p>说明: 你可以假设&nbsp;n&nbsp;不小于 2 且不大于 58。</p>
</li>
</ul>
</blockquote>
<p>这道题在我的做法中dp数组表示的是i这个数字的最大拆分乘积，初始化<code>dp[2] = 1</code>,这是易知的，而在一些题解中就把dp[0]和dp[1]也进行了初始化，这么做当然也可以AC，但是dp数组的意义已经模糊了，暂不谈题目限制`$$2 \le n \ge 58$$,就按照题目说明，0和1就不能拆分为两个正整数的和，更不用谈乘积，虽然这么初始化问题也不大，但是我感觉要透过题去理解，要明白dp数组的含义，下标表示了什么更为重要，下面是本题C++代码：</p>
<pre><code class="CPP">class Solution {
public:
    int integerBreak(int n) {
        vector&lt;int&gt; dp(n + 1);
        dp[2] = 1;
        for (int i = 3; i &lt;= n ; i++) {
            for (int j = 1; j &lt; i - 1; j++) {
                dp[i] = max(dp[i], max((i - j) * j, dp[i - j] * j));
            }
        }
        return dp[n];
    }
};
</code></pre>
<ul>
<li>时间复杂度：O(n^2)</li>
<li>空间复杂度：O(n)</li>
</ul>
<h2 id="背包问题2022-08-10"><a href="#背包问题2022-08-10" class="headerlink" title="背包问题2022.08.10"></a>背包问题2022.08.10</h2><blockquote>
<p>转载自<a target="_blank" rel="noopener" href="https://programmercarl.com/">代码随想录</a>，侵删</p>
</blockquote>
<p>建议按照如下顺序观看：<br><a target="_blank" rel="noopener" href="https://kpl0111.github.io/blog/2022/08c8776b35.html">背包理论基础01背包1</a><br><a target="_blank" rel="noopener" href="https://kpl0111.github.io/blog/2022/08517e3a8f.html">背包理论基础01背包2</a><br><a target="_blank" rel="noopener" href="https://kpl0111.github.io/blog/2022/08806f2082.html">背包基础理论完全背包</a><br><a target="_blank" rel="noopener" href="https://kpl0111.github.io/blog/2022/0871e46bfb.html">背包基础理论多重背包</a></p>
<h2 id="01背包和完全背包2022-08-13"><a href="#01背包和完全背包2022-08-13" class="headerlink" title="01背包和完全背包2022.08.13"></a>01背包和完全背包2022.08.13</h2><blockquote>
<p>背包问题大概描述是：有一个承重m的背包，旁边有一堆物品共n个，每个物品重量不一，价值不易，weight[i]和value[i]分别代表第i个物品的重量和价值，求背包最多能装下物品的价值</p>
<blockquote>
<p>01背包是说每个物品只有一个，怎么装价值最大<br>完全背包是说每种物品有无限多个，怎么装价值最大，比如一辆卡车去进货，每种货占的空间不同，价值也不同，但是每种货数量是不限制的，怎么装，一卡车货价值最大</p>
</blockquote>
</blockquote>
<h3 id="01背包-二维-先物品后背包"><a href="#01背包-二维-先物品后背包" class="headerlink" title="01背包-二维-先物品后背包"></a>01背包-二维-先物品后背包</h3><p>采用二维数组遍历必然要考虑遍历顺序，是先遍历背包，还是先遍历物品，不妨设先遍历物品，那么循环内层遍历的就是背包了</p>
<p>当对于num[i]，无非两种情况:</p>
<ol>
<li>背包可以装的下，继续装那么价值就是<code>dp[i - 1][j - weight[i]] + value[i]</code>,表示未装<code>num[i]</code>物品前，背包最大容量必然是<code>j - weight[i]</code>（因为还要装第<code>i</code>件物品，所以预留出<code>weight[i]</code>的空间，剩下的空间可以装的最大值就是<code>dp[i - 1][j - weight[i]]</code>）</li>
<li>装不下，背包里面物品价值还是不变，就是<code>dp[i - 1][j]</code></li>
</ol>
<p>那么二者取最大值，就是i件物品容量为j的背包可以装的物品最大价值，依次递推即可</p>
<pre><code class="cpp">for (int i = 0; i &lt; num[i]; i++) {
    for (int j = weight[i]; j &lt;= target; j++) { //j从weight[i]开始，是因为小于weight[i]的背包根本装不下第i件物品，所以一定都是dp[i - 1][j]
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
    }
}
</code></pre>
<h3 id="01背包-二维-先背包后物品"><a href="#01背包-二维-先背包后物品" class="headerlink" title="01背包-二维-先背包后物品"></a>01背包-二维-先背包后物品</h3><p>转变一下遍历顺序二维时是完全可以的，观察上述递归公式可以发现，dp[i][j]完全是由上一行的值和weight数组和value数组决定的，而后面两个是已知的，所以不管按照什么顺序遍历，dp[i][j]仅由其左上位置数值决定，所以遍历顺序怎么样都可以，下面给出先遍历背包，后遍历物品的代码：</p>
<pre><code class="cpp">for (int j = 0; j &lt;= target; j++) {
    for (int i = 1; i &lt; nums.size(); i++) {
        if (j &lt; weight[i]) dp[i][j] = dp[i - 1][j]; //装不下
        else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); //装得下
    }
}
</code></pre>
<h3 id="01背包-一维"><a href="#01背包-一维" class="headerlink" title="01背包-一维"></a>01背包-一维</h3><p>前面也已经说明过了，dp[i][j]只依赖上一行的数值，所以我们可以将dp数组降为一维，节约空间，但是要注意，二维的时候我们依赖的是上一行（i - 1行）的值，降为一维后，当不做任何变化时，当前数值就是上一行数值，但是如果j还是从前往后遍历，那么当为j时，从0 ~ （j - 1）的数值已经变为新值了，不再是上一行的值，所以不能从前往后遍历，而当我们从后往前遍历则是可以的，原因也很简单，前面说过，dp[i][j]只依赖左上方的值，当降为一维数组之后，就是只依赖左方的值，而我们从后往前遍历，左方的值一直都未变化，当然是可以的，代码如下：</p>
<pre><code class="cpp">for(int i = 0; i &lt; weight.size(); i++) { // 遍历物品
    for(int j = bagWeight; j &gt;= weight[i]; j--) { // 遍历背包容量
        dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
    }
}
</code></pre>
<p>顺序在这里也很重要，一定要是先物品后背包容量，反之则不行</p>
<p>观察二维时候的递推公式</p>
<pre><code class="c++">dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
</code></pre>
<p>dp[i][j]依赖的是上一行，不是上一列，是<code>j - weight[i]</code>列，所以滚动数组完全不可行</p>
<h3 id="完全背包"><a href="#完全背包" class="headerlink" title="完全背包"></a>完全背包</h3><p>完全背包相比01背包就是物品可以用无数次，仅仅在01背包的基础上略微修改即可，从前往后遍历即可</p>
<p>上文知道，01背包从后往前遍历是为了保证每个物品仅仅使用一次，而完全背包可以用无数次，从前往后即可，代码如下：</p>
<pre><code class="cpp">// 先遍历物品，再遍历背包
for(int i = 0; i &lt; weight.size(); i++) { // 遍历物品
    for(int j = weight[i]; j &lt;= bagWeight ; j++) { // 遍历背包容量
        dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);

    }
}

// 先遍历背包，再遍历物品
for(int j = 0; j &lt;= bagWeight; j++) { // 遍历背包容量
    for(int i = 0; i &lt; weight.size(); i++) { // 遍历物品
        if (j - weight[i] &gt;= 0) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
    }
    cout &lt;&lt; endl;
}
</code></pre>
<p>至于遍历顺序，则同01背包</p>
<h3 id="关于排列和组合"><a href="#关于排列和组合" class="headerlink" title="关于排列和组合"></a>关于排列和组合</h3><p>完全背包往往会涉及到排列和组合的问题</p>
<p>就比如这个<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/combination-sum-iv/">组合总数Ⅳ</a>,求数字总和为target</p>
<p>这道题目要求数字顺序不一样也是一种组合，其本质上就是排列问题，那么使用循环遍历时就需要注意遍历顺序了</p>
<p>先物品后背包肯定是行不通的，因为物品只能按顺序出现，比如说{1， 3}，不可能出现{3，1}，因为物品是在外侧循环，只能按照数组顺序出现，所以说不能使用先物品后背包的方式，而要用先背包后物品才可以</p>
<p>一句话总结就是先物品后背包是<strong>组合</strong> ， 先背包后物品是<strong>排列</strong></p>
<h3 id="多重背包"><a href="#多重背包" class="headerlink" title="多重背包"></a>多重背包</h3><p>多重背包其实跟01背包如出一辙，只是原本数量为1的物品现在数量不再一定为1了，而是由一个数量数组nums记录每件物品的数量有多少，针对多重背包，有以下两种解决方法：</p>
<ol>
<li>把多重背包拆分为01背包，把同一种物品拆分为一个一个的，问题就简化为了01背包，按照01背包的解决方法即可</li>
<li>在双重遍历的时候内嵌一个for循环，遍历这个物品对应的数量即可，本质上还是把同一个物品拆分为了一个一个的</li>
</ol>
<h3 id="背包总结"><a href="#背包总结" class="headerlink" title="背包总结"></a>背包总结</h3><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://code-thinking-1253855093.file.myqcloud.com/pics/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%981.jpeg" alt="" title="">
                </div>
                <div class="image-caption"></div>
            </figure>

<p>图源代码随想录知识星球 (opens new window)成员：<a target="_blank" rel="noopener" href="https://wx.zsxq.com/dweb2/index/footprint/844412858822412">海螺人</a></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新：<time datetime="2024-01-04T02:41:48.345Z" itemprop="dateUpdated">2024-01-04 10:41:48</time>
</span>


        
        
        
    </div>
    <footer>
        <div onclick="location.href='https://nefelibata.icu'">
            <img src="/blog/img/avatar.jpg" alt="Nefelibata">
            <a>Nefelibata</a>
        </div>
    </footer>
</blockquote>

        
    <div class="page-reward">
        <nav class="myreward">
            <a id="rewardBtn" href="javascript:;"><span>打&nbsp;赏</span><span>装成好像很多人打赏的样子</span></a>
        </nav>
    </div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/Leetcode/" rel="tag">Leetcode</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://nefelibata.icu/2022/0826e7783f.html&title=《LeetCode笔记-动态规划》 — Nefelibata&pic=https://nefelibata.icu/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://nefelibata.icu/2022/0826e7783f.html&title=《LeetCode笔记-动态规划》 — Nefelibata&source=动规" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://nefelibata.icu/2022/0826e7783f.html" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《LeetCode笔记-动态规划》 — Nefelibata&url=https://nefelibata.icu/2022/0826e7783f.html&via=https://nefelibata.icu" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://nefelibata.icu/2022/0826e7783f.html" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/2022/08c8776b35.html" id="post-prev" class="post-nav-link">
        <h4 class="title" >
          上一篇：背包理论基础01背包1
        </h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/2022/072a2449dc.html" id="post-next" class="post-nav-link">
        <h4 class="title" data-hover="下一篇：LeetCode笔记-贪心算法">下一篇：LeetCode笔记-贪心算法</h4>
      </a>
    </div>
  
</nav>


</article>

</div>

        <footer class="footer">
    <div class="footer-content">
        <span class="power">
            <i class="icon icon-lg icon-copyright"></i>
            2021
            <i class="icon icon-lg icon-heart"></i>
            <a href="https://nefelibata.icu">nefelibata.icu</a>
        </span>
    </div>
</footer>

    </main>
    
        
<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        <span>感谢您的鼓励支持！</span>
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" data-img="/blog/img/dog.png" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/blog/img/wechat.png" data-alipay="/blog/img/alipay.png">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechatPay">&nbsp;&nbsp;微信&nbsp;&nbsp;</span>
                <span class="reward-toggle-item alipayPay">支付宝</span>
            </div>
        </label>
        
        <i class="icon icon-caret-up"></i>
    </div>
</div>


    
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://nefelibata.icu/2022/0826e7783f.html&title=《LeetCode笔记-动态规划》 — Nefelibata&pic=https://nefelibata.icu/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://nefelibata.icu/2022/0826e7783f.html&title=《LeetCode笔记-动态规划》 — Nefelibata&source=动规" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://nefelibata.icu/2022/0826e7783f.html" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《LeetCode笔记-动态规划》 — Nefelibata&url=https://nefelibata.icu/2022/0826e7783f.html&via=https://nefelibata.icu" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://nefelibata.icu/2022/0826e7783f.html" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p class="wechatshare">扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <!-- waves按钮特效 -->
<script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>

<!-- 主题配置脚本 -->
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: true };
</script>

<!-- jquery -->
<script src="/blog/js/jquery.min.js?v=3.0"></script>

<!-- 搜索 -->

<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item waves-block waves-effect" onclick="location.href='{path}'">
    <div class="title ellipsis" title="{title}">{title}</div>
</li>
</template>


<!-- main博客脚本 -->
<script src="/blog/js/main.min.js?v=3.0" ></script>

<!-- 动画&配置 -->
<script src="/blog/js/script.min.js?v=3.0" ></script>

<!-- 脚本管理 -->
<script>

if(window.innerWidth > 800){
	/* 3D标题 */
	$(".content-header").on("mousemove", threedee);

	/* 底部追随鼠标 */
	$(".footer").hover(2);

	/* gotop键的涟漪 */
	$("#gotop").hover(1);

	/* 赞赏的粒子雨 */
	$("#reward").hover(3);

	/* 微信公众号的底部渲染 */
	$("#wechat").hover(4);

    /* 标题跳动 */
    $(".archivestitle").bumpyText();

	/* 图片点击放大 */
	const postimg = jQuery(".post-content img:not(.github-emoji)");
	postimg.on("click",function(){

		mask.classList.add("in");
		main.classList.add("Mask");
		menu.classList.add("Mask");
		var myimg = this.cloneNode(true);
		myimg.classList.add("imgShow");

		setTimeout(function(){
			jQuery(myimg).animate({
				opacity:"1"
			},1000);
		},0);

		document.body.appendChild(myimg);

		myimg.onclick=function(){
			document.body.removeChild(myimg);
			mask.classList.remove("in");
			main.classList.remove("Mask");
			menu.classList.remove("Mask");
		};

	});

}

/* 名字跳动 */
$("#name").bumpyText();


/* 网站运行时间 */
// setInterval(function () {
// 	setTime("2021/3/27");
// }, 1000);

/* 文章块的淡出 */
postshow();

/* 座右铭 */
// 
//    getHitokoto();
// 


/* 粘贴提示 */
G($(".post-content"), location.href, "Nefelibata");


/* 控制台 */
if (window.console && window.console.log) {
	setTimeout(function () {
		console.log("\n %c Nefelibata %c  © kpl0111  http://nefelibata.icu \n\n", "color:#FFFFFB;background:#1abc9c;padding:5px 0;border-radius:.5rem 0 0 .5rem;", "color:#FFFFFB;background:#080808;padding:5px 0;border-radius:0 .5rem .5rem 0;");
	}, 0);
}

</script>




<!-- 公式渲染 -->

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
    }
});
</script>

<script async src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML" async></script>



<!-- 不蒜子 -->
<!--  -->

</body>
</html>
